<?php
if(@$_POST['open'] != 'please'){
	exit;
}
include_once('../../php/include_all.php');
?>
<div class="row-fluid sortable">
    <div class="box span12 geo-box">
        <div class="box-header well">
            <h2><i class="icon-home"></i>User</h2>
        </div>
        <div class="box-content">
            <form id="formUSER">
                <input type="hidden" name="act" value="simpan_skpd">
                <input type="hidden" id="id_user" name="id_skpd" value="0"> 
                <table class="table-form">
                    <tbody>
                        
                        <tr>
                            <td valign="middle">Nama SKPD</td>
                            <td valign="middle">:</td>
                            <td>
                                <input type="text" name="nama_skpd" id="nama_skpd" class="xxlarge">
                            </td>
                        </tr>
                        <tr>
                            <td valign="middle">Nama Kepala SKPD</td>
                            <td valign="middle">:</td>
                            <td>
                                <input type="text" name="nama_kepala" id="nama_kepala" class="xxlarge">
                            </td>
                        </tr>
                        <tr>
                            <td valign="middle">NIP</td>
                            <td valign="middle">:</td>
                            <td>
                                <input type="text" name="nip" id="nip" class="xxlarge">
                            </td>
                        </tr>
                        <tr>
                            <td valign="middle">User Level</td>
                            <td valign="middle">:</td>
                            <td>
                                <select name="user_level" id="user_level">
                                    <?php
                                    $level = get_datas("select * from user_level order by id_uslevel");
                                    foreach ($level as $level) {
                                         ?>
                                        <option value="<?php echo $level['id_uslevel']; ?>"><?php echo $level['uslevel']; ?></option>
                                        <?php
                                    }
                                    ?>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td></td>
                            <td>
                                <a class="btn btn-primary btn-simpan-skpd"><div id="loader"></div>&nbsp;Simpan</a><span id="result"></span>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </form>

            <hr/>
            <table class="table table-bordered geo-table table-hover" width="100%">
                <thead> 
                    <tr>
                        <td width="5%">No</td>
                        <td width="10%">Kode SKPD</td>
                        <td width="20%">Nama SKPD</td>
                        <td width="20%">Nama Kepala SKPD</td>
                        <td width="5%">NIP</td>
                        <td width="10%">Username</td>
                        <td width="10%">Password</td>
                        <td width="10%">Level User</td>
                        <td width="10%">Action</td>
                    </tr>
                </thead>
                <tbody id="tampilSKPD">
                    <?php
                    $x=1;
                    $skpd = get_datas("select id_skpd, kode_skpd, nama_skpd, nama_kepala_skpd, nip, username, password, uslevel from mus_skpd s, user_level u where s.id_uslevel=u.id_uslevel order by nama_skpd  ");
                    foreach ($skpd as $skpd) {
                        ?>
                        <tr>
                            <td align="center"><?php echo $x?></td>
                            <td><?php echo $skpd['kode_skpd'] ?></td>
                            <td><?php echo $skpd['nama_skpd'] ?></td>
                            <td><?php echo $skpd['nama_kepala_skpd'] ?></td>
                            <td><?php echo $skpd['nip'] ?></td>
                            <td><?php echo $skpd['username'] ?></td>
                            <td><?php echo $skpd['password'] ?></td>
                            <td align="center"><?php echo $skpd['uslevel'] ?></td>
                            <td>
                                <a class="btn btn-danger bt-hapus" name="<?php echo $skpd['id_skpd']; ?>"><i class="icon-trash icon-white"></i></a>
                                <a class="btn btn-info bt-edit" name="<?php echo $skpd['id_skpd']; ?>"><i class="icon-edit icon-white"></i></a>
                            </td>
                        </tr>
                        <?php
                        $x++;
                    }
                    ?>
                </tbody>
            </table>    
        </div>
    </div>
</div>
<script>
var url = 'admin/master_user/user_save.php';
    var rslt = $('#result');
    $('.btn-simpan-skpd').click(function(){
        var btn = $(this);
        var load = $('#loader');
        
        load.addClass('spinner pull-left');
        btn.removeClass('btn-primary').addClass('btn-info');
        
        var form = $('#formUSER');
        var data = form.serializeArray();
        var post = $.post(url, data);
        post.done(function(res){
            var result = res.split('__');
            
            if(result.length==3){
                if(result[0]=='success'){
                    rslt.html('<font color="green">Data Tersimpan...</font>');
                    btn.removeClass('btn-info').addClass('btn-primary');
                    setTimeout(function(){
                        rslt.html('');
                    },1500);
                
                    var tbody = $('#tampilSKPD');
                    tbody.html(result[2]);
                    init();
                    $("#nama_skpd").val("");
                    $("#nama_kepala").val("");
                    $("#nip").val("");
                    document.getElementById("user_level").selected = true;

                }else{
                    rslt.html('<font color="red">'+res+'</font>');
                }
            }else{
                rslt.html('<font color="red">'+res+'</font>')
            }
            load.removeClass();
            btn.removeClass('.btn-info').addClass('.btn-primary');
        });
    });
    
    function init(){
        $('.bt-hapus').click(function(){
            var btn = $(this);
            var pid = this.name;
            var load = $(this);
            load.html('<div class="spinner"></div>');
            bootbox.confirm("sumpe lu mau ngehapus item ini?", function(hsl){
                if(hsl==true){
                    if(pid!=0){
                        var post = $.post(url,{act:"hapus_skpd",id_skpd:pid});
                        post.done(function(res){
                            var hasil = res.split('__');
                            //alert(hasil.length);
                            if(hasil.length == 2){
                                if(hasil[0] == 'success'){
                                    rslt.html('<font color="green">Data Telah Dihapus...</font>');
                                    setTimeout(function(){
                                        rslt.html('');
                                    }, 1500);
                                    var tbody = $('#tampilSKPD');
                                    tbody.html(hasil[1]);
                                    init();
                                    //btn.parent().parent().remove();
                                }
                            }
                        });
                        load.html('<i class="icon-trash icon-white">');
                    }
                }else{
                    load.html('<i class="icon-trash icon-white">');
                }
            });
        });
   
        $('.bt-edit').click(function(){
            var idskpd = this.name;
            var form = $('#formUSER');
            var post = $.post(url,{act:'ubah_skpd',id_skpd:idskpd});
            //var post = $.post(url,{act:'ubah_skpd',(id_skpd<iki variabel):(idskpd<iki value});            
            post.done(function(res){
                var value = res.split('__');  
                //alert(res);
                $('#id_user').val(value[0]);
                form.find('input[name="nama_skpd"]').val(value[1]);
                form.find('input[name="nama_kepala"]').val(value[2]);
                form.find('input[name="nip"]').val(value[3]);
                form.find('select[name="user_level"]').val(value[4]);  
                //$("#user_level").val(value[5]);
            });
        });
    }
    init();
</script>